Passed
Pull Request — master (#19)
by Muhammad Dyas
01:43
created

NewPollFormCard   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 134
dl 0
loc 175
rs 10
c 0
b 0
f 0

9 Functions

Rating   Name   Duplication   Size   Complexity  
B buildOptionSwitchSection 0 29 5
A buildHelpText 0 5 1
A buildFooter 0 8 1
A create 0 5 2
A topicInput 0 8 1
B buildCloseConfigSection 0 60 4
A buildSections 0 5 1
A optionInput 0 9 1
A buildTopicInputSection 0 13 3
1
import BaseCard from './BaseCard';
2
import {ClosableType, PollForm} from '../helpers/interfaces';
3
import {MAX_NUM_OF_OPTIONS} from '../config/default';
4
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
5
import {offsetToTimezone} from '../helpers/time';
6
7
export default class NewPollFormCard extends BaseCard {
8
  private config: PollForm;
9
  private timezone: chatV1.Schema$TimeZone;
10
11
  constructor(config: PollForm, timezone: chatV1.Schema$TimeZone | undefined = undefined) {
12
    super();
13
    this.config = config;
14
    if (timezone && timezone.offset) {
15
      this.timezone = timezone;
16
    } else {
17
      this.timezone = {offset: 0, id: 'GMT'};
18
    }
19
  }
20
21
  create() {
22
    this.buildSections();
23
    this.buildFooter();
24
    return this.card;
25
  }
26
27
  buildSections() {
28
    this.buildTopicInputSection();
29
    this.buildOptionSwitchSection();
30
    this.buildCloseConfigSection();
31
  }
32
33
  buildTopicInputSection() {
34
    const widgets = [];
35
    widgets.push(this.buildHelpText());
36
    widgets.push(this.topicInput(this.config.topic));
37
    for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) {
38
      const choice = this.config.choices?.[i];
39
      widgets.push(this.optionInput(i, choice));
40
    }
41
    this.card.sections!.push({
42
      'collapsible': true,
43
      'uncollapsibleWidgetsCount': 6,
44
      widgets,
45
    });
46
  }
47
48
  buildOptionSwitchSection() {
49
    this.card.sections!.push({
50
      'widgets': [
51
        {
52
          'decoratedText': {
53
            'bottomLabel': 'If this checked the voters name will be not shown',
54
            'text': 'Anonymous voter',
55
            'switchControl': {
56
              'controlType': 'SWITCH',
57
              'name': 'is_anonymous',
58
              'value': '1',
59
              'selected': this.config.anon ?? false,
60
            },
61
          },
62
          'horizontalAlignment': 'CENTER',
63
        },
64
        {
65
          'decoratedText': {
66
            'bottomLabel': 'After the poll is created, other member can add more option',
67
            'text': 'Allow to add more option(s)',
68
            'switchControl': {
69
              'controlType': 'SWITCH',
70
              'name': 'allow_add_option',
71
              'value': '1',
72
              'selected': this.config.optionable ?? true,
73
            },
74
          },
75
          'horizontalAlignment': 'CENTER',
76
        },
77
      ],
78
    });
79
  }
80
81
  buildCloseConfigSection() {
82
    const widgets: chatV1.Schema$GoogleAppsCardV1Widget[] = [
83
      {
84
        'selectionInput': {
85
          'type': 'DROPDOWN',
86
          'label': 'Allow to manually close poll',
87
          'name': 'type',
88
          'items': [
89
            {
90
              'text': 'Yes, but only creator',
91
              'value': '1',
92
              'selected': this.config.type === ClosableType.CLOSEABLE_BY_CREATOR,
93
            },
94
            {
95
              'text': 'Yes, anyone can close',
96
              'value': '2',
97
              'selected': this.config.type === ClosableType.CLOSEABLE_BY_ANYONE,
98
            },
99
            {
100
              'text': 'No, I want unclosable poll',
101
              'value': '0',
102
              'selected': this.config.type === ClosableType.UNCLOSEABLE,
103
            },
104
          ],
105
        },
106
        'horizontalAlignment': 'START',
107
      },
108
      {
109
        'decoratedText': {
110
          'topLabel': '',
111
          'text': 'Automatic close poll at certain time',
112
          'bottomLabel': 'The schedule time will show up',
113
          'switchControl': {
114
            'controlType': 'SWITCH',
115
            'name': 'is_autoclose',
116
            'value': '1',
117
            'selected': this.config.autoclose ?? false,
118
            'onChangeAction': {
119
              'function': 'new_poll_on_change',
120
              'parameters': [],
121
            },
122
          },
123
        },
124
      }];
125
    if (this.config.autoclose) {
126
      const timezone = offsetToTimezone(this.timezone.offset!);
127
      const nowMs = Date.now() + this.timezone.offset! + 18000000;
128
      widgets.push(
129
        {
130
          'dateTimePicker': {
131
            'label': 'Close schedule time ' + timezone,
132
            'name': 'close_schedule_time',
133
            'type': 'DATE_AND_TIME',
134
            'valueMsEpoch': nowMs.toString(),
135
          },
136
        });
137
    }
138
    this.card.sections!.push({
139
      widgets,
140
    });
141
  }
142
143
  buildHelpText() {
144
    return {
145
      textParagraph: {
146
        text: 'Enter the poll topic and up to 10 choices in the poll. Blank options will be omitted.',
147
      },
148
    };
149
  }
150
151
  topicInput(topic: string) {
152
    return {
153
      textInput: {
154
        label: 'Topic',
155
        type: 'MULTIPLE_LINE',
156
        name: 'topic',
157
        value: topic,
158
      },
159
    };
160
  }
161
162
  optionInput(
163
    index: number, value: string): chatV1.Schema$GoogleAppsCardV1Widget {
164
    return {
165
      textInput: {
166
        label: `Option ${index + 1}`,
167
        type: 'SINGLE_LINE',
168
        name: `option${index}`,
169
        value: value || '',
170
      },
171
    };
172
  }
173
174
  buildFooter() {
175
    this.card.fixedFooter = {
176
      'primaryButton': {
177
        'text': 'Submit',
178
        'onClick': {
179
          'action': {
180
            'function': 'start_poll',
181
          },
182
        },
183
      },
184
    };
185
  }
186
}
187